import java.util.LinkedList;
import java.util.Queue;
/*用两个队列实现栈*/
public class MyStackUseQueue {
    Queue<Integer> queue1 = new LinkedList<>();
    Queue<Integer> queue2 = new LinkedList<>();

    public void push(int x) {
        if (queue1.isEmpty()){
            queue2.offer(x);
        }else {
            queue1.offer(x);
        }
    }

    public int pop() {
        if (queue1.isEmpty()) {
            int size = queue2.size();
            for (int i = 0; i < size-1 ; i++) {
                queue1.offer(queue2.poll());
            }
            return queue2.poll();
        } else {
            int size = queue1.size();
            for (int i = 0; i < size-1 ; i++) {
                queue2.offer(queue1.poll());
            }
            return queue1.poll();
        }
    }
    public int top() {
        if (queue1.isEmpty()) {
            int size = queue2.size();
            for (int i = 0; i < size-1 ; i++) {
                queue1.offer(queue2.poll());
            }int x = queue2.peek();
            queue1.offer(queue2.poll());
            return x;
        } else {
            int size = queue1.size();
            for (int i = 0; i < size-1 ; i++) {
                queue2.offer(queue1.poll());
            }
            int y = queue1.peek();
            queue2.offer(queue1.poll());
            return y;
        }
        }


    public boolean empty() {
        if (queue1.isEmpty()){
            return queue2.isEmpty();
        }else {
            return queue1.isEmpty();
        }
    }
}
